package com.akk.module.app.service;

import com.akk.module.app.entity.AppBanner;
import com.akk.module.app.entity.AppBannerExample;
import com.akk.module.app.mapper.AppBannerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AppBannerService {
    @Autowired
    AppBannerMapper appBannerMapper;
    @Autowired
    AppBannerExample appBannerExample;

    public List<AppBanner> findByParam(Map param){
        appBannerExample.clear();
        AppBannerExample.Criteria criteria = appBannerExample.createCriteria();
        if (param.containsKey("bannerId"))
            criteria.andBannerIdEqualTo(param.get("bannerId").toString());
        if (param.containsKey("appId"))
            criteria.andAppIdEqualTo(param.get("appId").toString());
        if (param.containsKey("bannerTag"))
            criteria.andBannerTagEqualTo(param.get("bannerTag").toString());
        return appBannerMapper.selectByExample(appBannerExample);
    }

    public Map appBanner(String appId){
        Map<String,String> param = new HashMap<>();
        param.put("appId",appId);
        List<AppBanner> banners = findByParam(param);
        Set<String> tags = new HashSet<>();
        for (AppBanner banner:banners) {
            tags.add(banner.getBannerTag());
        }
        Map<String,Object> result = new HashMap<>();
        for (String tag:tags) {
            List<AppBanner> appBanners = new ArrayList<>();
            for (AppBanner banner:banners) {
                if (banner.getBannerTag().equals(tag))
                    appBanners.add(banner);
            }
            result.put(tag,appBanners);
        }
        return result;
    }

    public AppBanner findById(String bannerId){
        return appBannerMapper.selectByPrimaryKey(bannerId);
    }

    public void save(AppBanner appBanner){
        appBannerMapper.insert(appBanner);
    }

    public int update(AppBanner appBanner){
        return appBannerMapper.updateByPrimaryKey(appBanner);
    }

    public int delete(String bannerId){
        return appBannerMapper.deleteByPrimaryKey(bannerId);
    }
}
